using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class UiCraftList : MonoBehaviour, IPointerDownHandler
{
    private Ui ui;
    private GameObject craftSlotPrefab;
    public List<ItemDataEquipment> craftEquipments;

    private void Awake()
    {
        craftSlotPrefab = Resources.Load<GameObject>("Prefabs/Ui/UiCraftSlot");
        ui = FindAnyObjectByType<Ui>();
    }

    private void Start()
    {
        SetupDefaultCraftWindow();
        transform.parent.GetChild(0).GetComponent<UiCraftList>().SetupCraftList();
    }

    public void SetupCraftList()
    {
        for (int i = ui.CraftSlotsParent.childCount - 1; i >= 0; i--)
        {
            Destroy(ui.CraftSlotsParent.GetChild(i).gameObject);
        }

        foreach (ItemDataEquipment equipment in craftEquipments)
        {
            GameObject slotObject = Instantiate(craftSlotPrefab, ui.CraftSlotsParent);
            slotObject.GetComponent<UiCraftSlot>().Setup(equipment);
        }
    }

    public void SetupDefaultCraftWindow()
    {
        if (!craftEquipments[0]) return;
        ui.UiCraftWindow.Setup(craftEquipments[0]);
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        SetupCraftList();
    }
}